/**
 * Created by scenic on 2017/7/15.
 */

var start, scene, spaceRoot;
;

var BusinessTestUtils = {

    getStart: function () {
        if (start) {
            start.dispose();
        }

        document.getElementById("main_test").style.display = "block";
        var container = document.getElementById("container");
        var canvas = document.getElementById("rd_three");
        start = new GSThree.StartNB({canvas: canvas, container: container});
        return start;
    },

    getScene: function () {
        if (scene) {
            return scene;
        }
        scene = this.getStart().newScene("main");
        scene.sceneColor = "#000000"
        new GSThree.LightConfig(scene).addAmbientLight().addDirectionLight();

        function refresh() {
            requestAnimationFrame(refresh);
            start.clearCanvas();
            start.renderMultipleScene();
        }

        refresh();

        return scene;
    },
    addToScene: function (space) {


        space = space == undefined ? spaceRoot : space;

        if (scene == undefined) {
            this.getScene();
        }
        //增加mesh
        space.traverse(function () {
            if (this.spaceMesh.isAddToScene)
                scene.add(this.spaceMesh)
            this.faces.forEach(function (face) {
                if (face.getAssociateMesh().isAddToScene)
                    scene.add(face.getAssociateMesh())
            })

            // this.updateSpaceMeshMaterial();

        })

        GSThree.Utils.showSceneResource(scene, 'resource');
    },
    hideAllFrontPlank: function () {
        spaceRoot.traverse(function () {
            this.frontFace.getAssociateMesh().visible = false;
        })
    },

    showSpaceWithLine: function () {
        spaceRoot.traverse(function () {
            GSThree.Utils.transparentMesh(this.getAssociateMesh(), true);
        })
    }
}


describe("Test Space Method", function () {
    var gsbuild = GSResource;

    beforeEach(function () {
        // spaceRoot = GSResource.SpaceUtils.createRootBoxSpace(1400,900,500);
        spaceRoot = gsbuild.SpaceUtils.createRootBoxSpace(1400,900,500);
    })

    it("Test Space.getBeginFaceByDirection", function () {
        spaceRoot.updateSpaceMeshAndFaces();
        expect(spaceRoot.getBeginFaceByDirection(new THREE.Vector3(1, 0, 0))).toBe(spaceRoot.leftFace);
        expect(spaceRoot.getBeginFaceByDirection(new THREE.Vector3(-1, 0, 0))).toBe(spaceRoot.rightFace);
        expect(spaceRoot.getBeginFaceByDirection(new THREE.Vector3(0, 1, 0))).toBe(spaceRoot.bottomFace);
        expect(spaceRoot.getBeginFaceByDirection(new THREE.Vector3(0, -1, 0))).toBe(spaceRoot.topFace);
        expect(spaceRoot.getBeginFaceByDirection(new THREE.Vector3(0, 0, 1))).toBe(spaceRoot.backFace);
        expect(spaceRoot.getBeginFaceByDirection(new THREE.Vector3(0, 0, -1))).toBe(spaceRoot.frontFace);

    })


    it("AdvanceUpdateSegment", function () {

        var scene = BusinessTestUtils.getScene();
        spaceRoot.addMeshToScene(scene)
        spaceRoot.setSegment("1:2:1", new THREE.Vector3(1, 0, 0));
        spaceRoot.traverse(function () {
            this.faces.forEach(function (p1, p2, p3) {
                p1.getAssociateMesh().isAddToScene = true;
            })
        })
        spaceRoot.updateSegment();
        //
        var s1 = spaceRoot.children_space[0];
        s1.setSegment("1:1:1", new THREE.Vector3(0, 1, 0))
        s1.updateSegment();
        //
        s1.traverse(function (f) {
            f.getAssociateMesh().isAddToScene = true;
        })

        s1.leftFace.getAssociateMesh().isAddToScene = false;
        s1.rightFace.getAssociateMesh().isAddToScene = false;
        s1.topFace.getAssociateMesh().isAddToScene = true;
        s1.bottomFace.getAssociateMesh().isAddToScene = false;
        s1.frontFace.getAssociateMesh().isAddToScene = false;
        s1.backFace.getAssociateMesh().isAddToScene = false;
        s1.updateSegment();

        spaceRoot.traverse(function () {
            this.addMeshToScene(scene);
        })
        GSThree.Utils.showSceneResource(scene, 'resource');
        pending("")
    })


    it("updatePlankByMesh", function () {
        var scene = BusinessTestUtils.getScene();
        spaceRoot.updateSpaceMeshAndFaces();
        spaceRoot.updatePlankByMesh();
        spaceRoot.traverse(function () {
            this.addMeshToScene(scene);
            this.faces.forEach(function (face) {
                scene.add(face.getAssociateMesh());
            })

        })
        GSThree.Utils.showSceneResource(scene, 'resource');
        pending("");
    })
    it("updatePlankDimensionWidthDefault_最基本的测试", function () {
        spaceRoot.updateSpaceMeshAndFaces();
        spaceRoot.updatePlankByMesh();
        spaceRoot.faces.forEach(function (f) {
            f.getAssociateMesh().isAddToScene = true;
        })
        spaceRoot.updatePlankDimensionWidthDefault();


        var scene = BusinessTestUtils.getScene();
        spaceRoot.traverse(function () {
            this.addMeshToScene(scene);
            this.faces.forEach(function (face) {
                scene.add(face.getAssociateMesh());
            })

            this.updateSpaceMeshMaterial();
        })
        GSThree.Utils.showSceneResource(scene, 'resource');

        pending("");
    })

    it("updatePlankDimensionWidthDefault_嵌套测试", function () {
        spaceRoot.setSegment("1:2:1", new THREE.Vector3(1, 0, 0));
        spaceRoot.updateSegment();
        spaceRoot.faces.forEach(function (f) {
            f.getAssociateMesh().isAddToScene = true;
        })
        spaceRoot.frontFace.associatePlank.isDoor = false;
        spaceRoot.updateSegment();
        spaceRoot.updatePlankDimensionWidthDefault();

        var s1 = spaceRoot.children_space[0];
        s1.setSegment("1:1:1", new THREE.Vector3(0, 1, 0));
        s1.faces.forEach(function (f) {
            f.getAssociateMesh().isAddToScene = true;
        })
        s1.frontFace.associatePlank.isDoor = false;
        s1.updateSegment();
        s1.updatePlankDimensionWidthDefault();

        s1.traverse(function () {
            this.faces.forEach(function (f) {
                f.getAssociateMesh().isAddToScene = true;
            })
            this.frontFace.associatePlank.isDoor = false;
            this.updatePlankDimensionWidthDefault();
        })

        BusinessTestUtils.showSpaceWithLine();
        BusinessTestUtils.addToScene();
        pending("");
    })


    it("Test Space.updatePlankDoor", function () {
        spaceRoot.setSegment("1:2:1", new THREE.Vector3(1, 0, 0));
        spaceRoot.updateSegment();
        spaceRoot.faces.forEach(function (f) {
            f.getAssociateMesh().isAddToScene = true;
        })
        spaceRoot.updateSegment();
        spaceRoot.updatePlankDimensionWidthDefault();
        spaceRoot.updatePlankDoor();

        var childrenSpace0 = spaceRoot.children_space[0];
        childrenSpace0.setSegment("1:1:1", new THREE.Vector3(0, 1, 0));
        childrenSpace0.updateSegment();
        childrenSpace0.faces.forEach(function (f) {
            f.getAssociateMesh().isAddToScene = true;
        })
        childrenSpace0.updateSegment();
        childrenSpace0.updatePlankDimensionWidthDefault();
        childrenSpace0.updatePlankDoor();


        var childrenSpace0_0 = childrenSpace0.children_space[0];
        childrenSpace0_0.faces.forEach(function (f) {
            f.getAssociateMesh().isAddToScene = true;
        })
        childrenSpace0_0.updatePlankDimensionWidthDefault();
        childrenSpace0_0.updatePlankDoor();


        BusinessTestUtils.addToScene(spaceRoot);
        BusinessTestUtils.showSpaceWithLine();

        pending("");
    })

    it("测试板子碰撞", function () {
        spaceRoot.updateSpaceMeshAndFaces();
        spaceRoot.faces.forEach(function (f) {
            f.getAssociateMesh().isAddToScene = false;
        })
        spaceRoot.leftFace.getAssociateMesh().isAddToScene = true;
        spaceRoot.topFace.getAssociateMesh().isAddToScene = true;
        spaceRoot.updatePlankDimensionWidthDefault();
        spaceRoot.updatePlankDoor();

        var plankLeft = spaceRoot.leftFace.getAssociateMesh();
        var plankTop = spaceRoot.topFace.getAssociateMesh();



        function intersect(obj1, obj2) {
            var b1 = new THREE.Box3().setFromObject(obj1);
            var b2 = new THREE.Box3().setFromObject(obj2);
            b1.intersect(b2);
            var size = b1.getSize();
            if(size.x == 0 && size.y ==0 && size.z == 0){
                return false;
            }else {
                return true;
            }

        }

        spaceRoot.traverse(function (space) {
            space.faces.forEach(function (f) {
                var mesh = f.getAssociateMesh();
                if(mesh !== plankLeft) {
                    console.log(plankLeft.name +"  " + mesh.name);
                    if(intersect(plankLeft,mesh)) {

                    }
                }

            })
        })






        BusinessTestUtils.addToScene(spaceRoot);
        BusinessTestUtils.showSpaceWithLine();


        pending("");
    })


})